<?php

	include_once '../com/mss/model/Monastery.php';
	include_once '../com/mss/accessor/MonasteryAccessor.php';

	class MonasteryService {
	
		public function save($file) {
			$monasteries = array();
			$handle = fopen($file,"r");
	
			if (($handle = fopen($file, "r")) !== FALSE) {
				while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
					$name = $data[0];
					$location = $data[1];
					$telephone = $data[2];
					$address = "";//$data[3];
					
					$monastery = new Monastery("", $name, $location, $address, $telephone);
					array_push($monasteries, $monastery);
				}
				fclose($handle);
			} else {
				echo "I could not read it";
			}
			$monastery_accessor = new MonasteryAccessor();
			$monastery_accessor->saveAll($monasteries);
		}
		
		public function selectAll() {
			$monestery_accessor = new MonasteryAccessor();
			return $monestery_accessor->selectAll();
		}
		
		public function view($id) {
			$monestery_accessor = new MonasteryAccessor();
			return $monestery_accessor->select($id);
		}
		
		public function update(Monastery $monastery) {
			$errors = array();
			$results = array();
			if (count($errors, 1) == 0) {
				$monestery_accessor = new MonasteryAccessor();
				$monestery_accessor->update($user);
				$results['success'] = "YES";
				$results['message'] = "Monastery successfully updated!!!";	
			} else {
				$results['success'] = "NO";
				$results['errors'] = $errors;
			}
			return $results;			
		}
	}

?>